<?php
/**
 * 检查管理员是否存在
 * @param $sql
 * @return multitype
 */
function checkAdmin($sql){
    return fetchOne($sql);
}

/**
 * 检测是否有管理员登录
 */
function checkLogin(){
    if (isset($_SESSION['adminId'])==''&& $_COOKIE['adminId']==""){
        alertMes('请先登录','login.php');
    }
}

/**
 * 退出登录
 */
function logout(){
    $_SESSION=array();
    if(isset($_COOKIE[session_name()])){
        setcookie(session_name(),"",time()-1);
    }
    if(isset($_COOKIE['adminId'])){
        setcookie("adminId","",time()-1);
    }
    if(isset($_COOKIE['adminName'])){
        setcookie("adminName","",time()-1);
    }
    session_destroy();
    header("location:login.php");
}

/**
 * 添加管理员
 * @return string
 */
function addAdmin(){
    $arr=$_POST;
    $arr['password']=md5(123456);
    if(insert("imooc_admin",$arr)){#封装方法：插入数据
        $mes="添加成功!<br/><a href='addAdmin.php'>继续添加</a>|<a href='listAdmin.php'>查看管理员列表</a>";
    }else{
        $mes="添加管理员失败!<br/><a href='addAdmin.php'>重新添加</a>";
    }
    return $mes;
}

/**
 * 获取管理员列表
 * @return multitype
 */
function getAdminList(){
    $sql = 'select id,username,email from imooc_admin';
    $list = fetchAll($sql);
    return $list;
}

/**
 * 分页
 * @param $curr @当前页
 * @param int $limit @每页总条数
 * @return multitype
 */
function getAdminPage($curr,$limit=2){
    $sql = 'select * from imooc_admin';
    global $total;#总条数
    $total = getResultNum($sql);
    global $page;#总页数
    $page = ceil($total/$limit);
    if ($curr<1||$curr==null||!is_numeric($curr)){
        $curr = 1;
    }
    if ($curr>=$page){
        $curr = $page;
    }
    $offset = ($curr-1)*$limit;
    $sql = "select id,username,email from imooc_admin limit {$offset},{$limit}";
    $rows = fetchAll($sql);
    return $rows;
}

/**
 * 修改管理员信息
 * @param $id
 */
function editAdmin($id){
    $arr = $_POST;
    $arr['password'] = md5($_POST['password']);
    if (update("imooc_admin",$arr,"id={$id}")){
        $msg="编辑成功!<br/><a href='listAdmin.php'>查看管理员列表</a>";
    }else{
        $msg="编辑失败!<br/><a href='listAdmin.php'>请重新修改</a>";
    }
    return $msg;
}

/**
 * 删除管理员
 * @param $id
 * @return string
 */
function delAdmin($id){
    if (delete("imooc_admin","id={$id}")){
        $msg="删除成功!<br/><a href='listAdmin.php'>查看管理员列表</a>";
    }else{
        $msg="删除失败!<br/><a href='listAdmin.php'>请重新删除</a>";
    }
    return $msg;
}

/**
 * 添加用户
 * @return string
 */
function addUser(){
    $arr = $_POST;
    $arr['password'] = md5(isset($arr['password']) ? $arr['password'] :123456);
    $arr['regTime'] = time();
    $uploadFile = uploadFile("../uploads");
    if ($uploadFile && is_array($uploadFile)){
        $arr['face'] = $uploadFile[0]['name'];
    }else{
        return '添加失败<a href="addUser.php">重新添加</a>|<a href="listUser.php">查看用户列表</a>';
    }
    if (insert('imooc_user',$arr)){
        $msg = "添加成功！<br><a href='addUser.php'>继续添加</a>|<a href='listUser.php'>查看用户列表</a>";
    }else{
        $msg = "添加失败！<br><a href='addUser.php'>重新添加</a>|<a href='listUser.php'>查看用户列表</a>";
    }
    return $msg;
}

/**
 * 删除用户
 * @param $id
 * @return string
 */
function delUser($id){
    $sql = 'select face from imooc_user WHERE id='.$id;
    $row = fetchOne($sql);
    $face = $row['face'];
    if (file_exists("../uploads/".$face)){
        unlink("../uploads/".$face);
    }
    if (delete("imooc_user","id={$id}")){
        $msg = "删除成功！<br><a href='listUser.php'>查看用户列表</a>";
    }else{
        $msg = "删除失败！<br><a href='listUser.php'>请重新删除</a>";
    }
    return $msg;
}

/**
 * 编辑用户
 * @param $id
 * @return string
 */
function editUser($id){
    $arr = $_POST;
    $arr['password'] = md5($_POST['password']);
    if (update("imooc_user",$arr,"id={$id}")){
        $msg = "编辑成功！<br><a href='listUsser.php'>查看用户列表</a>";
    }else{
        $msg = "编辑失败！<br><a href='listUsser.php'>请重新编辑</a>";
    }
    return $msg;
}

/**
 * 分页
 * @param $curr @当前页
 * @param int $limit @每页条数
 * @return @multitype @返回类型
 */
function getUserPage($curr,$limit=2){
    $sql = 'select id,username,email,activeFlag from imooc_user';
    global $total;#总条数
    $total = getResultNum($sql);
    global $page;#总页数
    $page = ceil($total/$limit);
    if ($curr<1||$curr==null||!is_numeric($curr)){
        $curr = 1;
    }
    if ($curr>=$page){
        $curr = $page;
    }
    $offset = ($curr-1)*$limit;
    $sql = "select id,username,email,activeFlag from imooc_user limit {$offset},{$limit}";
    $rows = fetchAll($sql);
    return $rows;
}